package com.qnw.CardGroupManagement.util;

 //识别二维码
import android.graphics.*;
import android.graphics.drawable.*;
import com.google.zxing.*;
import com.google.zxing.common.*;
import com.google.zxing.qrcode.*;
import com.qnw.CardGroupManagement.*;
import java.util.*;

public class QRCodeUtils
 {

	public static String getStringFromQRCode(Drawable drawable) {
		String httpString = null;

		Bitmap bmp = ImageUtils.drawableToBitmap(drawable);
		byte[] data = ImageUtils.getYUV420sp(bmp.getWidth(), bmp.getHeight(), bmp);
		// 处理
		try {
			Hashtable<DecodeHintType, Object> hints = new Hashtable<DecodeHintType, Object>();
			hints.put(DecodeHintType.TRY_HARDER, Boolean.TRUE);
			hints.put(DecodeHintType.POSSIBLE_FORMATS, BarcodeFormat.QR_CODE);
			PlanarYUVLuminanceSource source = new PlanarYUVLuminanceSource(data, 
																		   bmp.getWidth(), 
																		   bmp.getHeight(),
																		   0, 0,
																		   bmp.getWidth(), 
																		   bmp.getHeight(),
																		   false);
			BinaryBitmap bitmap1 = new BinaryBitmap(new HybridBinarizer(source)); 
			QRCodeReader reader2= new QRCodeReader();
			Result result = reader2.decode(bitmap1, hints);

			httpString = result.getText();
		} catch (Exception e) {
			e.printStackTrace();
		} 

		bmp.recycle();
		bmp = null;

		return httpString;
	}
	}
